package com.uzai.console.service.order.impl;

import com.uzai.common.enums.PlatformTypeEnum;
import com.uzai.console.dto.dy.dyorder.DyOrderQuery;
import com.uzai.console.dto.jd.jdorder.JdOrderQuery;
import com.uzai.console.dto.ks.ksorder.KsOrderQuery;
import com.uzai.console.dto.locallive.elmorder.ElmOrderQuery;
import com.uzai.console.dto.locallive.mtorder.MtOrderQuery;
import com.uzai.console.dto.order.OrderQuery;
import com.uzai.console.dto.pdd.pddorder.PddOrderQuery;
import com.uzai.console.dto.tb.tborder.TbOrderQuery;
import com.uzai.console.dto.vph.vphorder.VphOrderQuery;
import com.uzai.console.entity.*;
import com.uzai.console.mapper.*;
import com.uzai.console.service.order.OrderOptService;
import com.uzai.console.vo.order.BaseOrderVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.util.ArrayList;
import java.util.List;

/**
 * 订单
 * @author liuqi
 * @date 2021年08月19日 16:50
 */
@Service
public class OrderOptServiceImpl implements OrderOptService {

    @Autowired
    private TbOrderMapper tbOrderMapper;
    @Autowired
    private JdOrderMapper jdOrderMapper;
    @Autowired
    private PddOrderMapper pddOrderMapper;
    @Autowired
    private VphOrderMapper vphOrderMapper;
    @Autowired
    private MtOrderMapper mtOrderMapper;
    @Autowired
    private ElmOrderMapper elmOrderMapper;
    @Autowired
    private DyOrderMapper dyOrderMapper;
    @Autowired
    private KsOrderMapper ksOrderMapper;


    //查询各平台订单记录
    public Page<BaseOrderVo> findBaseOrderList(OrderQuery orderQuery, Integer platform){

        //返回到页面分页对象
        Page<BaseOrderVo> pageVo = new Page<>();
        orderQuery.convert(pageVo);
        //新建返回到页面的LIST对象
        List<BaseOrderVo> baseOrderVoList = new ArrayList<>();

        if(platform.intValue() == PlatformTypeEnum.PT_TAOBAO.getId()){
            TbOrderQuery tbOrderQuery = new TbOrderQuery();
            BeanUtils.copyProperties(orderQuery, tbOrderQuery);
            tbOrderQuery.setMerId(orderQuery.getMerId());
            tbOrderQuery.setUserId(orderQuery.getUserId());
            Page<TbOrder> page = new Page<>();
            orderQuery.convert(page);
            List<TbOrder> tbOrderList = tbOrderMapper.findByList(page, tbOrderQuery);
            pageVo.setTotal(page.getTotal());
            if(null != tbOrderList && tbOrderList.size() > 0){
                for (TbOrder tbOrder : tbOrderList){
                    BaseOrderVo baseOrderVo = new BaseOrderVo();
                    BeanUtils.copyProperties(tbOrder, baseOrderVo);
                    baseOrderVoList.add(baseOrderVo);
                }
            }
        }else if(platform.intValue() == PlatformTypeEnum.PT_JD.getId()){
            JdOrderQuery jdOrderQuery = new JdOrderQuery();
            BeanUtils.copyProperties(orderQuery, jdOrderQuery);
            jdOrderQuery.setMerId(orderQuery.getMerId());
            jdOrderQuery.setUserId(orderQuery.getUserId());
            Page<JdOrder> page = new Page<>();
            orderQuery.convert(page);
            List<JdOrder> jdOrderList = jdOrderMapper.findByList(page, jdOrderQuery);
            pageVo.setTotal(page.getTotal());
            if(null != jdOrderList && jdOrderList.size() > 0){
                for (JdOrder jdOrder : jdOrderList){
                    BaseOrderVo baseOrderVo = new BaseOrderVo();
                    BeanUtils.copyProperties(jdOrder, baseOrderVo);
                    baseOrderVoList.add(baseOrderVo);
                }
            }
        }else if(platform.intValue() == PlatformTypeEnum.PT_PDD.getId()){
            PddOrderQuery pddOrderQuery = new PddOrderQuery();
            BeanUtils.copyProperties(orderQuery, pddOrderQuery);
            pddOrderQuery.setMerId(orderQuery.getMerId());
            pddOrderQuery.setUserId(orderQuery.getUserId());
            Page<PddOrder> page = new Page<>();
            orderQuery.convert(page);
            List<PddOrder> pddOrderList = pddOrderMapper.findByList(page, pddOrderQuery);
            pageVo.setTotal(page.getTotal());
            if(null != pddOrderList && pddOrderList.size() > 0){
                for (PddOrder pddOrder : pddOrderList){
                    BaseOrderVo baseOrderVo = new BaseOrderVo();
                    BeanUtils.copyProperties(pddOrder, baseOrderVo);
                    baseOrderVoList.add(baseOrderVo);
                }
            }
        }else if(platform.intValue() == PlatformTypeEnum.PT_VPH.getId()){
            VphOrderQuery vphOrderQuery = new VphOrderQuery();
            BeanUtils.copyProperties(orderQuery, vphOrderQuery);
            vphOrderQuery.setMerId(orderQuery.getMerId());
            vphOrderQuery.setUserId(orderQuery.getUserId());
            Page<VphOrder> page = new Page<>();
            orderQuery.convert(page);
            List<VphOrder> vphOrderList = vphOrderMapper.findByList(page, vphOrderQuery);
            pageVo.setTotal(page.getTotal());
            if(null != vphOrderList && vphOrderList.size() > 0){
                for (VphOrder vphOrder : vphOrderList){
                    BaseOrderVo baseOrderVo = new BaseOrderVo();
                    BeanUtils.copyProperties(vphOrder, baseOrderVo);
                    baseOrderVoList.add(baseOrderVo);
                }
            }
        }else if(platform.intValue() == PlatformTypeEnum.PT_MT_LM.getId()){
            MtOrderQuery mtOrderQuery = new MtOrderQuery();
            BeanUtils.copyProperties(orderQuery, mtOrderQuery);
            mtOrderQuery.setMerId(orderQuery.getMerId());
            mtOrderQuery.setUserId(orderQuery.getUserId());
            Page<MtOrder> page = new Page<>();
            orderQuery.convert(page);
            List<MtOrder> mtOrderList = mtOrderMapper.findByList(page, mtOrderQuery);
            pageVo.setTotal(page.getTotal());
            if(null != mtOrderList && mtOrderList.size() > 0){
                for (MtOrder mtOrder : mtOrderList){
                    BaseOrderVo baseOrderVo = new BaseOrderVo();
                    BeanUtils.copyProperties(mtOrder, baseOrderVo);
                    baseOrderVoList.add(baseOrderVo);
                }
            }
        }else if(platform.intValue() == PlatformTypeEnum.PT_ELM.getId()){
            ElmOrderQuery elmOrderQuery = new ElmOrderQuery();
            BeanUtils.copyProperties(orderQuery, elmOrderQuery);
            elmOrderQuery.setMerId(orderQuery.getMerId());
            elmOrderQuery.setUserId(orderQuery.getUserId());
            Page<ElmOrder> page = new Page<>();
            orderQuery.convert(page);
            List<ElmOrder> elmOrderList = elmOrderMapper.findByList(page, elmOrderQuery);
            pageVo.setTotal(page.getTotal());
            if(null != elmOrderList && elmOrderList.size() > 0){
                for (ElmOrder elmOrder : elmOrderList){
                    BaseOrderVo baseOrderVo = new BaseOrderVo();
                    BeanUtils.copyProperties(elmOrder, baseOrderVo);
                    baseOrderVoList.add(baseOrderVo);
                }
            }
        }else if(platform.intValue() == PlatformTypeEnum.PT_DY.getId()){
            DyOrderQuery dyOrderQuery = new DyOrderQuery();
            BeanUtils.copyProperties(orderQuery, dyOrderQuery);
            dyOrderQuery.setMerId(orderQuery.getMerId());
            dyOrderQuery.setUserId(orderQuery.getUserId());
            Page<DyOrder> page = new Page<>();
            orderQuery.convert(page);
            List<DyOrder> dyOrderList = dyOrderMapper.findByList(page, dyOrderQuery);
            pageVo.setTotal(page.getTotal());
            if(null != dyOrderList && dyOrderList.size() > 0){
                for (DyOrder dyOrder : dyOrderList){
                    BaseOrderVo baseOrderVo = new BaseOrderVo();
                    BeanUtils.copyProperties(dyOrder, baseOrderVo);
                    baseOrderVoList.add(baseOrderVo);
                }
            }
        }else if(platform.intValue() == PlatformTypeEnum.PT_KS.getId()){
            KsOrderQuery ksOrderQuery = new KsOrderQuery();
            BeanUtils.copyProperties(orderQuery, ksOrderQuery);
            ksOrderQuery.setMerId(orderQuery.getMerId());
            ksOrderQuery.setUserId(orderQuery.getUserId());
            Page<KsOrder> page = new Page<>();
            orderQuery.convert(page);
            List<KsOrder> ksOrderList = ksOrderMapper.findByList(page, ksOrderQuery);
            pageVo.setTotal(page.getTotal());
            if(null != ksOrderList && ksOrderList.size() > 0){
                for (KsOrder ksOrder : ksOrderList){
                    BaseOrderVo baseOrderVo = new BaseOrderVo();
                    BeanUtils.copyProperties(ksOrder, baseOrderVo);
                    baseOrderVoList.add(baseOrderVo);
                }
            }
        }

        pageVo.setRecords(baseOrderVoList);
        //将分页信息复制到返回到页面的分页MO当中
        return pageVo;
    }

}
